// 入驻申请
<template>
   <div class="search">
     <Row>
        <Card>
          <Row class="operation">
            <Button @click="add" icon="md-add" type="primary">添加</Button>
            <Button @click="handleSearch" icon="md-refresh">刷新</Button>
          </Row>
          <Modal title="查看大图" v-model="visible" class-name="fl-image-modal">
            <img :src="imgUrl" v-if="visible" style="width: 100%">
          </Modal>

          <Row>
            <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
          </Row>

          <Row type="flex" justify="end" class="page">
            <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize" @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]" size="small" show-total show-elevator show-sizer></Page>
          </Row>
        </Card>
     </Row>

     <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="1000">
      <Form ref="form" :model="form" :label-width="100" :rules="formValidate" >
        <FormItem label="活动名称" prop="title">
          <Input type="text" v-model="form.title" placeholder="请输入活动名称" />
        </FormItem>
        <FormItem label="活动图片" prop="pic">
          <upload-pic-input v-model="form.pic" style="width: 100%"></upload-pic-input>
        </FormItem>
        <FormItem label="活动跳转链接" prop="url">
          <Input type="text" v-model="form.url" placeholder="请输入活动跳转链接" />
        </FormItem>
        <FormItem label="活动详情">
          <editor id="editor" v-model="form.content" ref="editor"></editor>
        </FormItem>
        
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalVisible=false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
      </div>
    </Modal>
   </div> 
</template>

<script>
import uploadPicInput from "@/views/my-components/xboot/upload-pic-input"
import editor from "@/views/my-components/xboot/editor";
import { validateName } from "@/libs/validate";
export default {
  components: {
    editor,
    uploadPicInput
  },
  data(){
    return{ 
      modalType: 0,
      submitLoading: false,
      visible:false,
      total:0,
      modalTitle: "",
      modalVisible: false,
      loading: true, // 表单加载状态
      data:[],//表单数据
      searchForm:{
        pageSize:10,
        pageNumber:1,
      },
      form: {
        id: "",
        pic: "",
        title: "",
        content: "",
        url: ""
      },
      formValidate: {
        title: [{ required: true, message: "请输入活动名称", trigger: "blur" },
        { validator: validateName, trigger: "blur" }],
        pic: [{ required: true, message: "请选择活动图片", trigger: "change" }],
        url: [{ required: false, message: "请输入活动跳转链接", trigger: "blur" }],
      },
      columns: [
        {
          type: "index",
          width: 60,
          align: "center"
        },
        {
          title: "活动标题",
          key: "title",
          minWidth: 120,
          sortable: false,
        },
        {
          title: "活动图片",
          key: "pic",
          minWidth: 120,
          sortable: false,
          render: (h,params) => {
            return h("img", {
              attrs: {
                src: params.row.pic
              },
              style: {
                width: "40px",
                height: "30px"
              },
              on: {
                click: ()=> {
                  this.handleView(params.row.pic)
                }
              }
            })
          }
        },
        {
          title: "活动内容",
          key: "content",
          minWidth: 120,
          sortable: false,
          render: (h,params) => {
            return h('div',{
              domProps:{
                innerHTML:`${params.row.content}`
              }
            })
          }
        },
        {
          title: "活动跳转链接",
          key: "url",
          minWidth: 140,
          sortable: false,
        },
        {
          title: "所属店铺",
          key: "shopName",
          minWidth: 140,
          sortable: false,
        },
        {
          title: "创建时间",
          key: "createTime",
          minWidth: 140,
          sortable: false,
        },
        {
          title: "操作",
          width: 260,
          align: "center",
          fixed: "right",
          render: (h, params) => {
            return h("div", [
                h(
                  "Button",
                  {
                    props: {
                      type: "primary",
                      size: "small"
                    },
                    style: {
                        "marginRight": "5px"
                    },
                    on: {
                      click: () => {
                        this.edit(params.row);
                      }
                    }
                  },
                  "编辑"
                ),
                h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small"
                    },
                    on: {
                      click: () => {
                        this.remove(params.row);
                      }
                    }
                  },
                  "删除"
                ),
              ]);
          }
        }
      ],
      type: ""
      }
    },
    methods:{
      init(){
        this.getDataList()
      },
      handleView(url) {
        this.imgUrl = url;
        this.visible = true;
      },
      getDataList(){
        this.loading = true;
        this.postRequest("/storeActivity/getStoreActivityList",this.searchForm).then(res =>{
            this.loading = false
            if(res.success){
                var arr = res.result.data;
                for(let val of arr){
                  for(let i in val){
                    (val[i] === null) ? (val[i] = "-") : ""
                  }
                }
                this.data = arr;
                this.total = res.result.size;
            } else {
                this.data = [];
                this.total = 0;
            }
        })
      },
      //搜索查询
      handleSearch(){
        this.searchForm.pageNumber = 1;
        this.getDataList()
      },
      changeSort(e) {
        this.searchForm.sort = e.key;
        this.searchForm.order = e.order;
        if (e.order === "normal") {
          this.searchForm.order = "";
        }
        this.getDataList();
      },
      changePage(v) {
        this.searchForm.pageNumber = v;
        this.getDataList();
      },
      changePageSize(v) {
        this.searchForm.pageSize = v;
        this.getDataList();
      },
      clearSelectAll(){
        this.$refs.searchForm.resetFields()
        this.getDataList()
      },
      add () {
        this.modalTitle = "添加";
        this.modalType = 0;
        var data1 = "";
        this.$refs.editor.setData(data1)
        this.$refs.form.resetFields();
        this.modalVisible = true;
      },
      edit (v) {
        this.modalTitle = "编辑";
        this.modalType = 1;
        this.form.id = v.id;
        this.form.title = v.title;
        this.form.pic = v.pic;
        this.form.url = v.url;
        this.$refs.editor.setData(v.content);
        this.modalVisible = true;
      },
      remove (v) {
        this.$Modal.confirm({
            title: "删除",
            content: "确认删除所选的数据吗？",
            loading: true,
            onOk: ()=> {
                var data = {
                    ids: v.id
                }
                this.postRequest("/storeActivity/removeStoreActivity", data).then(res=> {
                    if (res.success) {
                        this.$Message.success("删除成功");
                        this.getDataList();
                        this.$Modal.remove();
                    } else {
                        this.$Message.error(res.message);
                        this.$Modal.remove();
                    }
                })
            }
        })
      },
      handleSubmit () {
        this.$refs.form.validate(valid=> {
          if (valid) {
            this.submitLoading = true;
            if (this.modalType === 0) {
              delete this.form.id
              this.postRequest("/storeActivity/insertOrderUpdateStoreActivity", this.form).then(res=> {
                this.submitLoading = false;
                if (res.success) {
                    this.$Message.success("审核成功");
                    this.getDataList();
                    this.modalVisible = false;
                } else {
                    this.$Message.success(res.message);
                    this.modalVisible = false;
                }
              })
            } else {
              this.postRequest("/storeActivity/insertOrderUpdateStoreActivity", this.form).then(res=> {
                this.submitLoading = false;
                if (res.success) {
                    this.$Message.success("审核成功");
                    this.getDataList();
                    this.modalVisible = false;
                } else {
                    this.$Message.success(res.message);
                    this.modalVisible = false;
                }
              })
            }
            
          }
        })
      }
    },
    mounted(){
      this.init()
    }
}
</script>

<style lang="less">
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-size: 13px;
        font-weight: 600;  
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        font-size: 13px;
        margin-left: 5px;
    }
}
</style>